package cycling.taxi.com.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.TextureMapView;
import com.baidu.mapapi.model.LatLng;

import cycling.taxi.com.R;
import cycling.taxi.com.person.ExitApplication;

/**
 * Created by Administrator on 2017/5/4.
 */
public class WebsiteMapAty extends AppCompatActivity {

    private TextureMapView mMapView;
    String TAG ="---BaiduMapFrgt";
    private BaiduMap mBaiduMap;
    private LocationClient mLocationClient;
    boolean isFirstLoc = true; // 是否首次定位

    private MyLocationConfiguration.LocationMode mCurrentMode;
    public MyLocationListenner myListener = new MyLocationListenner();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.frgt_website_map);
        ExitApplication.getInstance().addActivity(this);

        mMapView = (TextureMapView)findViewById(R.id.website_bmap);
        //获得Map控制器
        mBaiduMap = mMapView.getMap();
        initData();
    }

//    @Override
//    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
//        super.onCreate(savedInstanceState, persistentState);
//        setContentView(R.layout.frgt_website_map);
//        mMapView = (TextureMapView)findViewById(R.id.website_bmap);
//        LogUtil.w("mMapView="+mMapView);
//        //获得Map控制器
//        mBaiduMap = mMapView.getMap();
//
//        initData();
//    }


    private void initData() {
        mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;

        //开启定位图层
        mBaiduMap.setMyLocationEnabled(true);

        // 定位初始化
        mLocationClient = new LocationClient(this);

        //获取地图自定义图标
        BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.drawable.dot_focus);
        //配置地图坐标
        MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode, true, bitmapDescriptor);
        //设置坐标
        mBaiduMap.setMyLocationConfigeration(config);


        LocationClientOption option = new LocationClientOption();
        option.setOpenGps(true); // 打开gps
        option.setCoorType("bd09ll"); // 设置坐标类型
        option.setScanSpan(1000);//扫描时间
        mLocationClient.registerLocationListener(myListener);//设置位置监听
        mLocationClient.setLocOption(option);
        mLocationClient.start();
    }

    /**
     * 定位SDK监听函数
     */
    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMapView == null) {
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此处设置开发者获取到的方向信息，顺时针0-360====还有设置经纬度
                    .direction(100).latitude(29.806651)
                    .longitude(121.606983).build();

            mBaiduMap.setMyLocationData(locData);


            if (isFirstLoc) {
                isFirstLoc = false;
                //设置经纬度
                LatLng ll = new LatLng(29.806651,121.606983);

                MapStatus.Builder builder = new MapStatus.Builder();

                builder.target(ll).zoom(18.0f);

                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));

            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    @Override
    public void onResume() {
        Log.d(TAG,"onResume");
        mMapView.setVisibility(View.VISIBLE);
        mMapView.onResume();
        super.onResume();
    }



    @Override
    public void onPause() {
        Log.d(TAG,"onPause");
        mMapView.setVisibility(View.INVISIBLE);
        mMapView.onPause();
        super.onPause();

    }

    @Override
    public void onDestroy() {
        Log.d(TAG,"onDestroy");
        super.onDestroy();
        mMapView.onDestroy();
        // 退出时销毁定位
        mLocationClient.stop();
        // 关闭定位图层
        mBaiduMap.setMyLocationEnabled(false);
//        mMapView.onDestroy();
        mMapView = null;
    }
}
